package com.hcss.common.util;

import java.io.*;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

/**
 * @function : 字符串工具类
 */
public class StringUtil {
	
	public static final String EMPTY = "";
	
	public static final String NULL = null;
	
	public static final String DOT = ".";
	
	/**
	 * 首字母大写
	 * @param str
	 * @return
	 */
	public static String upperFirstChar(String str){
		if(EmptyUtil.isHaveEmpty(str)){
			return str;
		}
		
		return str.substring(0, 1).toUpperCase() + str.substring(1);
	}
	
	/**
	 * 首字母小写
	 * @param str
	 * @return
	 */
	public static String lowerFirstChar(String str){
		if(EmptyUtil.isHaveEmpty(str)){
			return str;
		}
		
		return str.substring(0, 1).toLowerCase() + str.substring(1);
	}

    /**
     * 把字符串数组转换为字符串
     * @param arr
     * @return
     */
    public final static String ArrayToString(String[] arr) {
        StringBuffer bf = new StringBuffer();
        for (int i = 0; i < arr.length; i++) {
            bf.append(arr[i]);
        }
        return bf.toString();
    }
    public final static Long[] StringToLongArray(String s, String split) {
        if(s==null){
            return null;
        }
        String[] sArray = s.split(split);
        Long[] lArray = new Long[sArray.length];
        for (int i = 0; i < sArray.length; i++) {
            lArray[i] = Long.valueOf(sArray[i]);
        }
        return lArray;
    }

    /**
     *
     * StreamToString:流转字符串. <br/>
     * @param in
     * @return
     */
    public static String InputStreamToString(InputStream in) throws IOException {
        StringBuilder buffer = new StringBuilder();
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(in,StandardCharsets.UTF_8 ));
            String line = null;
            while ((line = reader.readLine()) != null) {
                buffer.append(line);
            }
        } finally {
            if (null != reader) {
                reader.close();
            }
        }
        buffer.trimToSize();
        return buffer.toString();
    }

    // UTF8格式转码
    public static String encodeUtf8(String content) {
        try {
            return URLEncoder.encode(content, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            return "";
        }
    }

    // UTF8格式解码
    public static String decodeUtf8(String content) {
        try {
            return URLDecoder.decode(content, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            return "";
        }
    }
}
